package client

import (
	. "chess_platform/internal/common"
	"chess_platform/models/client"
	"chess_platform/models/pc"
	"encoding/json"
	"fmt"
	"time"
)

//[client]邮件及公告方面
type EmailController struct {
	GrantController
}

func (b *EmailController) URLMapping() {
	b.Mapping("List", b.List)
	b.Mapping("Read", b.Read)
	b.Mapping("Attachment", b.Attachment)
}

// @Title 获取邮件列表(15天以内的)
// @Description 获取邮件列表(15天以内的)
// @Param   cursor       body   int     true       "游标(即现在客户端读取到第几封邮件了,0-表示拉取所有的邮件)"
// @Success 0 {object} []common.Email 邮件列表
// @Failure 399
// @router /list  [post]
func (b *EmailController) List() {
	type Params struct {
		Cursor int64 `json:"cursor"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}

	emails, count, cursor, err := client.ListEmail(p.Cursor, b.Uid)
	if err != nil {
		LogClient.Error(b.DatabaseError(err.Error()))
		return
	}
	data := map[string]interface{}{
		"expire_time": time.Now().Unix() - EmailExpire, //告诉前端邮件什么时候可以清除掉了,不用再缓存了
		"cursor":      cursor,
		"email":       emails,
	}
	b.Res.Data = data
	b.Res.Count = count
	b.ResponseSuccess()
}

// @Title 读邮件
// @Description 读邮件
// @Param   eid     body    int     true      "邮件id"
// @Success 0 {string} 状态码
// @Failure 399
// @router /read [post]
func (b *EmailController) Read() {
	type Params struct {
		Eid int64 `json:"eid" valid:"Required"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}

	if !b.ValidParams(&p) {
		return
	}
	email := map[string]interface{}{
		"status": 1,
	}

	err := client.UpdateEmail(email, p.Eid, b.Uid)
	if err != nil {
		LogClient.Error(b.DatabaseError(err.Error()))
		return
	}
	b.ResponseSuccess()
}

// @Title 获取邮件附件
// @Description 获取邮件附件
// @Param   eid     body    int     true      "邮件id"
// @Success 0 {string} 状态码
// @Failure 399
// @router /attachment [post]
func (b *EmailController) Attachment() {
	type Params struct {
		Eid int64 `json:"eid" valid:"Required"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}

	email, err := client.AttachmentById(p.Eid, b.Uid)
	if err != nil {
		b.Res.Code = ErrNotFound
		b.Res.Msg = MsgErrEmailNotFound
		b.Response()
		return
	}
	if email == nil {
		b.ResponseFail("邮件不存在")
		return
	}
	if email.IsGet == 1 {
		b.ResponseFail("已经领取过了")
		return
	}
	//附件,type: 1-钻石,2-金币 [{"qty":10,"type":1},{"qty":20,"type":2}]
	var attach []Attachment
	err = json.Unmarshal([]byte(email.Attachment), &attach)
	if err != nil {
		b.ResponseFail("系统异常,领取失败")
		return
	}
	for _, a := range attach {
		if a.Type == 1 {
			ok := client.ChangeAccountValueFromLobby(b.Uid, a.Qty,
				0, 0, ACCTRAN_OPER_TYPE_MAIL, 0)
			if !ok {
				LogClient.Error(fmt.Sprintf("游戏服返回失败,玩家:%v领取附件失败", b.Uid))
				b.ResponseFail("系统异常,领取失败")
				return
			}
			//通过邮件领取的都是从系统赠送帐号那里扣
			err := pc.DoSystemDiamond(SysClientGiveUid, -a.Qty, b.Uid, email.OperateType, -1, Player)
			if err != nil {
				LogClient.Error(fmt.Sprintf("从客户端系统赠送帐号:%v扣钻石数:%v到玩家(ID:%v)失败,操作类型为:%v,失败日志:%v",
					SysClientGiveUid, a.Qty, b.Uid, email.OperateType, err))
			}
		}
	}

	updateEmail := map[string]interface{}{
		"is_get": 1,
	}
	err = client.UpdateEmail(updateEmail, p.Eid, b.Uid)
	if err != nil {
		LogClient.Error(b.DatabaseError(err.Error()))
		return
	}

	b.Res.Data = email
	b.ResponseSuccess("领取成功")

}

type Attachment struct {
	Qty  int64 `json:"qty"`
	Type int64 `json:"type"`
}
